package com.gable.report.controller;

import com.gable.report.common.util.ReturnResultUtil;
import com.gable.report.model.ChartsBaseDataView;
import com.gable.report.model.ChartsHospitalQuery;
import com.gable.report.model.User;
import com.gable.report.service.ChartsForHospitalBaseDataService;
import com.gable.report.service.SetRepeatRepairService;
import com.gable.report.service.UserService;
import com.sun.istack.internal.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lvdouStart on 2017/9/22.
 */
@Controller
@RequestMapping("/charts/hospital/index")
public class ChartsForHospitalIndexController {

    Logger logger = Logger.getLogger(ChartsForHospitalIndexController.class);

    @Autowired
    UserService userService;

    @Autowired
    SetRepeatRepairService setRepeatRepairService;

    @Autowired
    ChartsForHospitalBaseDataService chartsForHospitalBaseDataService;


    /**
     * @Author: 杨青桦
     * @Description: 医院科室主页概览框接口
     * @Date: 2017/9/22 16:28
     * @param userId
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/department-overview",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryAllAssetsForDepartmentIndex(String userId, ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryAllAssetsForDepartmentIndex(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }

    /** 
     * @Author: 杨青桦
     * @Description:  设备类型主页概览框接口
     * @Date: 2017/9/22 16:28
     * @param userId
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/type-overview",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryAllAssetsForDeviceIndex(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryAllAssetsForDeviceIndex(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }

    /**
     * @Author: 杨青桦
     * @Description:  设备型号主页概览框接口
     * @Date: 2017/9/22 16:28
     * @param userId
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/model-overview",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryAllAssetsForDeviceAgeLimit(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryAllAssetsForDeviceAgeLimit(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description:  医院科室接口
     * @Date: 2017/9/22 16:28
     * @param userId
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/department",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDepartments(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryDepartments(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description:  设备类型接口
     * @Date: 2017/9/22 16:28
     * @param userId
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/type",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceTypes(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryDeviceTypes(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description:  设备型号接口
     * @Date: 2017/9/22 16:28
     * @param userId
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/model",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceModels(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryDeviceModels(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description:  设备厂家接口
     * @Date: 2017/9/22 16:28
     * @param userId
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/brand",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceBrands(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryDeviceBrands(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description:  年份数据
     * @Date: 2017/9/22 16:28
     * @param userId
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/year-trend",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryYearTrend(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        Map<String,Object> map = new HashMap<>();
        try {
            List<ChartsBaseDataView> list = chartsForHospitalBaseDataService.queryYearTrend(hospitalId, chartsHospitalQuery);
            map.put("list",list);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }

    /**
     * @Author: 杨青桦
     * @Description:  具体设备接口
     * @Date: 2017/9/22 16:28
     * @param userId
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/device",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceInfo(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryDeviceInfoByModelName(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description: 查询新增或者报废设备信息
     * @Date: 2017/12/20 10:47
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/new-or-scrap",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryNewlyOrScrapAssetsInfo(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryNewlyOrScrapAssetsInfo(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


}
